<template>
  <div class="components-container">
    <sweet-modal ref="pdf" class="dialog" :sweetModalMaxWidth="1000">
      <h3>{{ order }}</h3>
      <iframe :src="printUrl" style="width:920px; height:500px;margin-top:20px;"></iframe>
      <div class="layui-form-item btnCenter">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </sweet-modal>
  </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>
<script>
import apiBase from "@/api/apiBase.js"
import sweetModal from "@/components/sweetModal"
import { manage } from "@/mixins/managePage.js"
export default {
  mixins: [manage],
  data() {
    return {
      printUrl: null,
      webSocket: null,
      id: null
    }
  },
  methods: {
    closeDialog() {
      this.$refs.pdf.close()
    },
    submit() {
      apiBase.DoService(
        {
          FRCMBDID: this.id,
          FileContentBase64: this.printUrl.split(",")[1]
        },
        apiBase.getApiAdress().JointDeclareProjct.CreateFilePdf,
        res => {
          this.$message({
            type: "success",
            message: "保存联办单成功"
          })
          this.$refs.pdf.close()
        }
      )
    },
    myBrowser() {
      var userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
      var isOpera = userAgent.indexOf("Opera") > -1
      if (isOpera) {
        return "Opera"
      } // 判断是否Opera浏览器
      else if (userAgent.indexOf("Firefox") > -1) {
        return "FF"
      } // 判断是否Firefox浏览器
      else if (userAgent.indexOf("Edge") > -1) {
        return "Edge"
      } // 判断是否Safari浏览器
      else if (userAgent.indexOf("Chrome") > -1) {
        return "Chrome"
      } else if (userAgent.indexOf("Safari") > -1) {
        return "Safari"
      } else {
        return "IE"
      } // 判断是否IE浏览器
    },
    onMessage(event) {
      var ret = event.data
      var obj = JSON.parse(ret)
      console.log(obj.ret)
      // alert(obj.type);
      if (obj.type == 3) {
        // 电子签名
        if (obj.ret == 0) {
          this.OnAfterGWQ_StartSign(
            obj.ret,
            obj.SignPdfBase64,
            obj.SignNameBase64,
            obj.FingerPrintBase64,
            obj.XML
          )
        } else {
          this.OnAfterCall(obj.ret)
        }
      }
    },
    OnAfterGWQ_StartSign(
      ErrorCode,
      SignPdfBase64,
      SignNameBase64,
      FingerPrintBase64,
      MixBase64
    ) {
      if (ErrorCode == 0) {
        this.$message({
          type: "success",
          message: "电子签名成功"
        })
        this.printUrl = "data:application/pdf;base64," + SignPdfBase64
        this.$refs.pdf.open()
      } else if (ErrorCode == -9) {
        this.$message({
          type: "warning",
          message: "取消电子签名"
        })
      } else {
        this.$message({
          type: "warning",
          message: "失败，返回错误码为" + ErrorCode
        })
      }
    },
    DoGWQ_StartSign(PDFPath, XmlPath, mylocation, VoiceStr, timeout, id) {
      this.id = id
      // if(PDFPath=="") PDFPath="D:\\test.pdf";
      if (this.myBrowser() == "IE") {
        var ret = GWQ.DoGWQ_StartSign(
          PDFPath,
          XmlPath,
          mylocation,
          VoiceStr,
          timeout
        )
        // OnAfterCall(ret);
      } else {
        var json = {}
        json.PDFPath = PDFPath
        json.XmlPath = XmlPath
        json.Location = mylocation
        json.Retain = VoiceStr
        json.Timeout = timeout
        json.type = 3
        var jsonStr = JSON.stringify(json)
        // alert(jsonStr);
        this.webSocket.send(jsonStr)
      }
    },
    OnAfterCall(ErrorCode) {
      if (ErrorCode == 0) {
        this.$message({
          type: "success",
          message: "成功"
        })
      } else if (ErrorCode == -1) {
        this.$message({
          type: "warning",
          message: "参数错误"
        })
      } else if (ErrorCode == -2) {
        this.$message({
          type: "warning",
          message: "超时"
        })
      } else if (ErrorCode == -3) {
        this.$message({
          type: "warning",
          message: "设备连接错误"
        })
      } else if (ErrorCode == -4) {
        this.$message({
          type: "warning",
          message: "发送数据失败"
        })
      } else if (ErrorCode == -5) {
        this.$message({
          type: "warning",
          message: "读取数据失败"
        })
      } else if (ErrorCode == -6) {
        this.$message({
          type: "warning",
          message: "文件操作失败"
        })
      } else if (ErrorCode == -7) {
        this.$message({
          type: "warning",
          message: "设备返回错误信息"
        })
      } else if (ErrorCode == -9) {
        this.$message({
          type: "warning",
          message: "取消"
        })
      } else {
        this.$message({
          type: "warning",
          message: "失败，返回错误码为" + ErrorCode
        })
      }
    }
  },
  created() {
    if (this.myBrowser() != "IE") {
      this.webSocket = new WebSocket("ws://localhost:1919")

      this.webSocket.onerror = function(event) {
        console.log("连接错误")
        onError(event)
      }

      this.webSocket.onopen = function(event) {
        console.log("连接成功")
        // alert("open"+event.data);
        // console.log(event);
      }

      this.webSocket.onclose = function(event) {
        console.log("服务不存在或者被关闭")
        // onClose(event);
      }
      // 是一个当收到来自服务器的消息时被调用
      this.webSocket.onmessage = event => {
        this.onMessage(event)
      }
    }
  },
  components: {
    sweetModal
  }
}
</script>
